# Standard data
df <- readRDS("./data/dataset/df.RDS")
tr <- read.tree("./data/tree/tree.treefile") 
pclustering <- readRDS("./data/asr_clustering/blbli_asr_clustering_df.RDS")
df <- df %>% left_join(.,pclustering)
# Matrices 
## KPC Plasmids 
KPC_info_clusters <- readRDS("./data/KPC_plasmid/KPC_containing_clusters_mat.RDS") %>% as.data.frame 
KPC_containing_clusters <- KPC_info_clusters %>% `colnames<-`(paste0(colnames(.),"_KPC")) %>% {ifelse(.=="KPC Plasmid",1,0)} %>% as.data.frame%>% select_if(colSums(.)>0)
KPC_clusters <- colnames(KPC_containing_clusters)
Non_KPC_clusters <- KPC_info_clusters %>% `colnames<-`(paste0(colnames(.),"_non_KPC")) %>% {ifelse(.=="Non-KPC plasmid",1,0)} %>% as.data.frame %>% select_if(colSums(.)>0)
df <- left_join(df,KPC_containing_clusters %>% mutate(isolate_no = rownames(.))) %>% left_join(Non_KPC_clusters %>% mutate(isolate_no = rownames(.)))

Gain & Loss Figure

traits_of_interest <- c("blbli_dich_num",KPC_clusters)   
trait_parent_child <- lapply(traits_of_interest,FUN=function(x){
  asr(df,tr,tip_name_var = 'isolate_no',pheno=x,model='MF') %>% .$parent_child_df
}) %>% `names<-`(traits_of_interest)
[1] "Performing model finding to use best fitting model"
You specified 'fixed.nodes=FALSE' but included a phy object with node labels. These node labels have been removed.
State distribution in data:
States: 0   1   
Counts: 354 58  
Beginning thorough optimization search -- performing 0 random restarts 
Finished. Inferring ancestral states using joint reconstruction. 
You specified 'fixed.nodes=FALSE' but included a phy object with node labels. These node labels have been removed.
State distribution in data:
States: 0   1   
Counts: 354 58  
Beginning thorough optimization search -- performing 0 random restarts 
Finished. Inferring ancestral states using joint reconstruction. 
[1] "Best model is: ARD"
You specified 'fixed.nodes=FALSE' but included a phy object with node labels. These node labels have been removed.
State distribution in data:
States: 0   1   
Counts: 354 58  
Beginning thorough optimization search -- performing 0 random restarts 
Finished. Inferring ancestral states using joint reconstruction. 
[1] "Performing model finding to use best fitting model"
You specified 'fixed.nodes=FALSE' but included a phy object with node labels. These node labels have been removed.
State distribution in data:
States: 0   1   
Counts: 377 35  
Beginning thorough optimization search -- performing 0 random restarts 
Finished. Inferring ancestral states using joint reconstruction. 
You specified 'fixed.nodes=FALSE' but included a phy object with node labels. These node labels have been removed.
State distribution in data:
States: 0   1   
Counts: 377 35  
Beginning thorough optimization search -- performing 0 random restarts 
Finished. Inferring ancestral states using joint reconstruction. 
[1] "Best model is: ARD"
You specified 'fixed.nodes=FALSE' but included a phy object with node labels. These node labels have been removed.
State distribution in data:
States: 0   1   
Counts: 377 35  
Beginning thorough optimization search -- performing 0 random restarts 
Finished. Inferring ancestral states using joint reconstruction. 
[1] "Performing model finding to use best fitting model"
You specified 'fixed.nodes=FALSE' but included a phy object with node labels. These node labels have been removed.
State distribution in data:
States: 0   1   
Counts: 361 51  
Beginning thorough optimization search -- performing 0 random restarts 
Finished. Inferring ancestral states using joint reconstruction. 
You specified 'fixed.nodes=FALSE' but included a phy object with node labels. These node labels have been removed.
State distribution in data:
States: 0   1   
Counts: 361 51  
Beginning thorough optimization search -- performing 0 random restarts 
Finished. Inferring ancestral states using joint reconstruction. 
[1] "Best model is: ARD"
You specified 'fixed.nodes=FALSE' but included a phy object with node labels. These node labels have been removed.
State distribution in data:
States: 0   1   
Counts: 361 51  
Beginning thorough optimization search -- performing 0 random restarts 
Finished. Inferring ancestral states using joint reconstruction. 
[1] "Performing model finding to use best fitting model"
You specified 'fixed.nodes=FALSE' but included a phy object with node labels. These node labels have been removed.
State distribution in data:
States: 0   1   
Counts: 386 26  
Beginning thorough optimization search -- performing 0 random restarts 
Finished. Inferring ancestral states using joint reconstruction. 
You specified 'fixed.nodes=FALSE' but included a phy object with node labels. These node labels have been removed.
State distribution in data:
States: 0   1   
Counts: 386 26  
Beginning thorough optimization search -- performing 0 random restarts 
Finished. Inferring ancestral states using joint reconstruction. 
[1] "Best model is: ER"
You specified 'fixed.nodes=FALSE' but included a phy object with node labels. These node labels have been removed.
State distribution in data:
States: 0   1   
Counts: 386 26  
Beginning thorough optimization search -- performing 0 random restarts 
Finished. Inferring ancestral states using joint reconstruction. 
[1] "Performing model finding to use best fitting model"
You specified 'fixed.nodes=FALSE' but included a phy object with node labels. These node labels have been removed.
State distribution in data:
States: 0   1   
Counts: 409 3   
Beginning thorough optimization search -- performing 0 random restarts 
Finished. Inferring ancestral states using joint reconstruction. 
You specified 'fixed.nodes=FALSE' but included a phy object with node labels. These node labels have been removed.
State distribution in data:
States: 0   1   
Counts: 409 3   
Beginning thorough optimization search -- performing 0 random restarts 
Finished. Inferring ancestral states using joint reconstruction. 
[1] "Best model is: ARD"
You specified 'fixed.nodes=FALSE' but included a phy object with node labels. These node labels have been removed.
State distribution in data:
States: 0   1   
Counts: 409 3   
Beginning thorough optimization search -- performing 0 random restarts 
Finished. Inferring ancestral states using joint reconstruction. 
[1] "Performing model finding to use best fitting model"
You specified 'fixed.nodes=FALSE' but included a phy object with node labels. These node labels have been removed.
State distribution in data:
States: 0   1   
Counts: 338 74  
Beginning thorough optimization search -- performing 0 random restarts 
Finished. Inferring ancestral states using joint reconstruction. 
You specified 'fixed.nodes=FALSE' but included a phy object with node labels. These node labels have been removed.
State distribution in data:
States: 0   1   
Counts: 338 74  
Beginning thorough optimization search -- performing 0 random restarts 
Finished. Inferring ancestral states using joint reconstruction. 
[1] "Best model is: ARD"
You specified 'fixed.nodes=FALSE' but included a phy object with node labels. These node labels have been removed.
State distribution in data:
States: 0   1   
Counts: 338 74  
Beginning thorough optimization search -- performing 0 random restarts 
Finished. Inferring ancestral states using joint reconstruction. 
[1] "Performing model finding to use best fitting model"
You specified 'fixed.nodes=FALSE' but included a phy object with node labels. These node labels have been removed.
State distribution in data:
States: 0   1   
Counts: 310 102 
Beginning thorough optimization search -- performing 0 random restarts 
Finished. Inferring ancestral states using joint reconstruction. 
You specified 'fixed.nodes=FALSE' but included a phy object with node labels. These node labels have been removed.
State distribution in data:
States: 0   1   
Counts: 310 102 
Beginning thorough optimization search -- performing 0 random restarts 
Finished. Inferring ancestral states using joint reconstruction. 
[1] "Best model is: ARD"
You specified 'fixed.nodes=FALSE' but included a phy object with node labels. These node labels have been removed.
State distribution in data:
States: 0   1   
Counts: 310 102 
Beginning thorough optimization search -- performing 0 random restarts 
Finished. Inferring ancestral states using joint reconstruction. 
[1] "Performing model finding to use best fitting model"
You specified 'fixed.nodes=FALSE' but included a phy object with node labels. These node labels have been removed.
State distribution in data:
States: 0   1   
Counts: 357 55  
Beginning thorough optimization search -- performing 0 random restarts 
Finished. Inferring ancestral states using joint reconstruction. 
You specified 'fixed.nodes=FALSE' but included a phy object with node labels. These node labels have been removed.
State distribution in data:
States: 0   1   
Counts: 357 55  
Beginning thorough optimization search -- performing 0 random restarts 
Finished. Inferring ancestral states using joint reconstruction. 
[1] "Best model is: ARD"
You specified 'fixed.nodes=FALSE' but included a phy object with node labels. These node labels have been removed.
State distribution in data:
States: 0   1   
Counts: 357 55  
Beginning thorough optimization search -- performing 0 random restarts 
Finished. Inferring ancestral states using joint reconstruction. 
[1] "Performing model finding to use best fitting model"
You specified 'fixed.nodes=FALSE' but included a phy object with node labels. These node labels have been removed.
State distribution in data:
States: 0   1   
Counts: 407 5   
Beginning thorough optimization search -- performing 0 random restarts 
Finished. Inferring ancestral states using joint reconstruction. 
You specified 'fixed.nodes=FALSE' but included a phy object with node labels. These node labels have been removed.
State distribution in data:
States: 0   1   
Counts: 407 5   
Beginning thorough optimization search -- performing 0 random restarts 
Finished. Inferring ancestral states using joint reconstruction. 
[1] "Best model is: ARD"
You specified 'fixed.nodes=FALSE' but included a phy object with node labels. These node labels have been removed.
State distribution in data:
States: 0   1   
Counts: 407 5   
Beginning thorough optimization search -- performing 0 random restarts 
Finished. Inferring ancestral states using joint reconstruction. 
[1] "Performing model finding to use best fitting model"
You specified 'fixed.nodes=FALSE' but included a phy object with node labels. These node labels have been removed.
State distribution in data:
States: 0   1   
Counts: 411 1   
Beginning thorough optimization search -- performing 0 random restarts 
Finished. Inferring ancestral states using joint reconstruction. 
You specified 'fixed.nodes=FALSE' but included a phy object with node labels. These node labels have been removed.
State distribution in data:
States: 0   1   
Counts: 411 1   
Beginning thorough optimization search -- performing 0 random restarts 
Finished. Inferring ancestral states using joint reconstruction. 
[1] "Best model is: ARD"
You specified 'fixed.nodes=FALSE' but included a phy object with node labels. These node labels have been removed.
State distribution in data:
States: 0   1   
Counts: 411 1   
Beginning thorough optimization search -- performing 0 random restarts 
Finished. Inferring ancestral states using joint reconstruction. 
[1] "Performing model finding to use best fitting model"
You specified 'fixed.nodes=FALSE' but included a phy object with node labels. These node labels have been removed.
State distribution in data:
States: 0   1   
Counts: 411 1   
Beginning thorough optimization search -- performing 0 random restarts 
Finished. Inferring ancestral states using joint reconstruction. 
You specified 'fixed.nodes=FALSE' but included a phy object with node labels. These node labels have been removed.
State distribution in data:
States: 0   1   
Counts: 411 1   
Beginning thorough optimization search -- performing 0 random restarts 
Finished. Inferring ancestral states using joint reconstruction. 
[1] "Best model is: ER"
You specified 'fixed.nodes=FALSE' but included a phy object with node labels. These node labels have been removed.
State distribution in data:
States: 0   1   
Counts: 411 1   
Beginning thorough optimization search -- performing 0 random restarts 
Finished. Inferring ancestral states using joint reconstruction. 
[1] "Performing model finding to use best fitting model"
You specified 'fixed.nodes=FALSE' but included a phy object with node labels. These node labels have been removed.
State distribution in data:
States: 0   1   
Counts: 375 37  
Beginning thorough optimization search -- performing 0 random restarts 
Finished. Inferring ancestral states using joint reconstruction. 
You specified 'fixed.nodes=FALSE' but included a phy object with node labels. These node labels have been removed.
State distribution in data:
States: 0   1   
Counts: 375 37  
Beginning thorough optimization search -- performing 0 random restarts 
Finished. Inferring ancestral states using joint reconstruction. 
[1] "Best model is: ARD"
You specified 'fixed.nodes=FALSE' but included a phy object with node labels. These node labels have been removed.
State distribution in data:
States: 0   1   
Counts: 375 37  
Beginning thorough optimization search -- performing 0 random restarts 
Finished. Inferring ancestral states using joint reconstruction. 
[1] "Performing model finding to use best fitting model"
You specified 'fixed.nodes=FALSE' but included a phy object with node labels. These node labels have been removed.
State distribution in data:
States: 0   1   
Counts: 410 2   
Beginning thorough optimization search -- performing 0 random restarts 
Finished. Inferring ancestral states using joint reconstruction. 
You specified 'fixed.nodes=FALSE' but included a phy object with node labels. These node labels have been removed.
State distribution in data:
States: 0   1   
Counts: 410 2   
Beginning thorough optimization search -- performing 0 random restarts 
Finished. Inferring ancestral states using joint reconstruction. 
[1] "Best model is: ARD"
You specified 'fixed.nodes=FALSE' but included a phy object with node labels. These node labels have been removed.
State distribution in data:
States: 0   1   
Counts: 410 2   
Beginning thorough optimization search -- performing 0 random restarts 
Finished. Inferring ancestral states using joint reconstruction. 
synchronous_changes_w_AA552 <- lapply(traits_of_interest %>% subset(.!="AA552_KPC"),FUN=function(x){
  phyloAMR::synchronous_detection(comparitor_parent_child_df = trait_parent_child[[x]],trait_parent_child_df = trait_parent_child[['AA552_KPC']],node_states = 'joint')
}) %>% do.call(rbind,.) %>% mutate(comparitor = traits_of_interest %>% subset(.!="AA552_KPC"))

synchronous_changes_w_AA552 %>% subset(synchronous_transitions_num>0)
  num_trait_gains synchronous_transitions_num synchronous_gains
1               7                           4    54,471,473,492
6               7                           5                  
7               7                           2                  
  synchronous_gains_num synchronous_gains_prop synchronous_gain_loss
1                     4              0.5714286                      
6                     0              0.0000000    54,471,473,492,519
7                     0              0.0000000               220,222
  synchronous_gain_loss_num synchronous_gain_loss_prop num_trait_losses
1                         0                  0.0000000                0
6                         5                  0.7142857                0
7                         2                  0.2857143                0
  synchronous_losses synchronous_losses_num synchronous_losses_prop
1                                         0                     NaN
6                                         0                     NaN
7                                         0                     NaN
  synchronous_loss_gain synchronous_loss_gain_num synchronous_loss_gain_prop
1                                               0                        NaN
6                                               0                        NaN
7                                               0                        NaN
      comparitor
1 blbli_dich_num
6      AA018_KPC
7      AA274_KPC
AA552_gain <- trait_parent_child$AA552_KPC %>% subset(gain ==1) %>% .$child
AA018_loss <- trait_parent_child$AA018_KPC %>% subset(loss ==1) %>% .$child
AA274_loss <- trait_parent_child$AA274_KPC %>% subset(loss ==1) %>% .$child
st258 <- ggtree(tr)
tree_obj <- st258   +
    geom_nodepoint(aes(fill = "AA552 KPC plasmid gain",shape="AA552 KPC plasmid gain",subset=(node %in% AA552_gain),size="AA552 KPC plasmid gain",legend=F))  +
    geom_tippoint(aes(fill = "AA552 KPC plasmid gain",shape="AA552 KPC plasmid gain",subset=(node %in% AA552_gain),size="AA552 KPC plasmid gain",legend=F)) +
    geom_nodepoint(aes(fill = "AA274 KPC plasmid loss",shape="AA274 KPC plasmid loss",subset=(node %in% AA274_loss),size="AA274 KPC plasmid loss",legend=F)) + 
    geom_tippoint(aes(fill = "AA274 KPC plasmid loss",shape="AA274 KPC plasmid loss",subset=(node %in% AA274_loss),size="AA274 KPC plasmid loss",legend=F))  +
    geom_nodepoint(aes(fill = "AA018 KPC plasmid loss",shape="AA018 KPC plasmid loss",subset=(node %in% AA018_loss),size="AA018 KPC plasmid loss",legend=F)) +
    geom_tippoint(aes(fill = "AA018 KPC plasmid loss",shape="AA018 KPC plasmid loss",subset=(node %in% AA018_loss),size="AA018 KPC plasmid loss",legend=F))  + scale_fill_manual(name="Gain/Loss Events",values = c("AA552 KPC plasmid gain" ="#91796A","AA018 KPC plasmid loss"="#97C466","AA274 KPC plasmid loss"="blue"),guide = guide_legend(title.position = "top",label.position = "bottom",ncol=1,order=1)) + 
    scale_shape_manual(name="Gain/Loss Events",values=c("AA552 KPC plasmid gain"=22,"AA018 KPC plasmid loss"=24,"AA274 KPC plasmid loss"=23),guide = guide_legend(title.position = "top",label.position = "bottom",ncol=1,order=1)) + scale_size_manual(name="Gain/Loss Events",values = c("AA552 KPC plasmid gain" =20,"AA018 KPC plasmid loss"= 16,"AA274 KPC plasmid loss"=16),guide = guide_legend(title.position = "top",label.position = "bottom",ncol=1,order=1))  

A. KPC Plasmid content